<template>
  <view class="y-collapse">
    <view
      class="y-collapse-lineTitle"
      :class="[isShow ? 'y-collapse-active' : '']"
      @click="showOrhide"
    >
      <span :class="[isShow ? 'select-style' : '']">{{ title }}</span>
      <svg
        class="totateIcon icon"
        :class="[isShow ? 'totateIcon-active' : '']"
        t="1633659587701"
        viewBox="0 0 1024 1024"
        version="1.1"
        xmlns="http://www.w3.org/2000/svg"
        p-id="2416"
        width="32"
        height="32"
      >
        <path
          d="M448 672c-6.4 0-19.2 0-25.6-6.4-12.8-12.8-12.8-32 0-44.8L531.2 512 422.4 409.6c-12.8-12.8-12.8-32 0-44.8s32-12.8 44.8 0l128 128c12.8 12.8 12.8 32 0 44.8l-128 128C467.2 672 454.4 672 448 672z"
          p-id="2417"
        ></path>
      </svg>
    </view>
    <view
      :style="{ height: getHeight }"
      ref="lineTitle-content"
      class="lineTitle-content"
    >
      <slot></slot>
    </view>
  </view>
</template>

<script>
export default {
  props: {
    title: {
      type: String,
      default: "",
    },
    index: {
      type: Number || String,
      default: 0,
    },
    isShowContent: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      scrollHeight: null,
      isShow: this.isShowContent,
    };
  },
  computed: {
    getHeight() {
      // return this.isShow ? this.scrollHeight + "px" : 0 + "px";
      return this.isShow ? "" : 0 + "px";
    },
  },
  methods: {
    showOrhide() {
      this.isShow = !this.isShow;
    },
    getScrollHeight() {
      this.scrollHeight = this.$refs["lineTitle-content"].$el.scrollHeight;
    },
  },

  mounted() {
    this.getScrollHeight();
  },
  updated() {
    this.getScrollHeight();
    this.$nextTick(() => {
      this.$emit("showOrhide", this.isShow);
    });
  },
};
</script>

<style lang="scss" scoped>
$transitionSet: all 0.3s;
.y-collapse {
  background: #fff;
  width: 100%;
  padding: 20upx 6upx;
  box-sizing: border-box;
  &-lineTitle {
    line-height: 50upx;
    padding-left: 30upx;
    height: 50upx;
    border-left: 6upx solid transparent;
    font-size: 14px;
    font-weight: bold;
    color: #303133;
    display: flex;
    justify-content: space-between;
    transition: $transitionSet;
    .totateIcon {
      transition: $transitionSet;
      transform: rotate(0deg);
    }
    .totateIcon-active {
      transform: rotate(90deg);
    }
    .select-style {
      color: $y-theme-color;
    }
  }
  &-active {
    border-left: 3px solid $y-theme-color !important;
  }
  .lineTitle-content {
    transition: $transitionSet;
    overflow: hidden;
    padding: 0 10upx 0 36upx;
  }
}
</style>
